package managedbean.SaleInquiry;

import entity.CRM.LineItem;
import entity.CRM.ProductPrice;
import entity.CRM.SaleInquiry;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.DiscountBeanLocal;
import session.stateless.CRM.ProductPriceBeanLocal;
import session.stateless.CRM.SaleInquiryBeanLocal;


@ManagedBean(name="editSaleInquiryManagerBean")
@ViewScoped
public class EditSaleInquiryManagerBean {

    public EditSaleInquiryManagerBean() {
    }
    
    @EJB
    private SaleInquiryBeanLocal saleInquiryBeanLocal;
    @EJB
    private ProductPriceBeanLocal productBeanLocal;
    @EJB
    private DiscountBeanLocal discountBeanLocal;
    
    private SaleInquiry saleInquiryToEdit;
    private String statusMessage;
    private Double finalPrice;

    public SaleInquiry getSaleInquiryToEdit() {
        return saleInquiryToEdit;
    }
    public void setSaleInquiryToEdit(SaleInquiry saleInquiryToEdit) {
        this.saleInquiryToEdit = saleInquiryToEdit;
    }
    
    public void initView(PhaseEvent event){
        if(saleInquiryToEdit==null){
            saleInquiryToEdit = (SaleInquiry)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedSaleInquiry");            
            this.setFinalPrice(saleInquiryToEdit.getTotalPrice());
        }
        
    }

    public Double getFinalPrice() {
        return finalPrice;
    }

    public void setFinalPrice(Double finalPrice) {
        this.finalPrice = finalPrice;
    }
    
    public void updateSaleInquiry(ActionEvent event){
        
        try{
             saleInquiryBeanLocal.updateSaleInquiry(saleInquiryToEdit.getId(), saleInquiryToEdit.getLineItems());
             statusMessage = "Sale Inquiry Updated Successfully"; 
        
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Update Sale Inquiry Result: " + statusMessage + " (Sale Inquiry ID is " + saleInquiryToEdit.getId().toString() + ")", "")); 
        }
        catch(Exception ex){
         ex.printStackTrace();   
        }
    }
    
    public void calculate(ActionEvent event)
    {
        Double temp_Price= 0.0;
        for (LineItem lineItem:saleInquiryToEdit.getLineItems())
        {
            ProductPrice temp_product= productBeanLocal.lookupRange(lineItem.getProductPrice().getProductType(), lineItem.getLineItemQuantity());
            lineItem.setProductPrice(temp_product);
            
            Double temp_discountRate= discountBeanLocal.calculateDiscountRate(lineItem.getProductPrice().getProductType(), lineItem.getLineItemQuantity());
            lineItem.setVolumeDiscountRate(temp_discountRate);
            
            Double temp_finalPrice=temp_product.getProductPrice() * lineItem.getLineItemQuantity() * (1-(saleInquiryToEdit.getCustomDiscount()+temp_discountRate)/100);
            lineItem.setFinalPrice(temp_finalPrice);
            temp_Price+=temp_finalPrice;
        }
        this.setFinalPrice(temp_Price);
        System.out.println(finalPrice);
    }
}
